home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / pm-utils / sleep.d / 99video < prev   
Encoding:
Text File  |  2009-04-07  |  5.2 KB  |  204 lines

  1. #!/bin/sh
  2. #
  3. # Copyright 2006-2007 Richard Hughes <richard@hughsie.com>
  4. # Copyright 2007 Peter Jones <pjones@redhat.com>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of version 2 of the GNU General Public License as
  8. # published by the Free Software Foundation.
  9.  
  10. # Handle video quirks.  If you are having suspend/resume issues,
  11. # troubleshooting using this hook is probably the best place to start.
  12. # If it weren't for video card quirks, suspend/resume on Linux would be 
  13. # a whole lot more stable.
  14.  
  15. . "${PM_FUNCTIONS}"
  16.  
  17. for opt in $PM_CMDLINE; do
  18.     case "${opt##--quirk-}" in # just quirks, please
  19.         dpms-on)        QUIRK_DPMS_ON="true" ;;
  20.         dpms-suspend)        QUIRK_DPMS_SUSPEND="true" ;;
  21.         radeon-off)        QUIRK_RADEON_OFF="true" ;;
  22.         reset-brightness)  QUIRK_RESET_BRIGHTNESS="true" ;;
  23.         s3-bios)        QUIRK_S3_BIOS="true" ;;
  24.         s3-mode)        QUIRK_S3_MODE="true" ;;
  25.         vbe-post)        QUIRK_VBE_POST="true" ;;
  26.         vbemode-restore)   QUIRK_VBEMODE_RESTORE="true" ;;
  27.         vbestate-restore)  QUIRK_VBESTATE_RESTORE="true" ;;
  28.         vga-mode3)        QUIRK_VGA_MODE_3="true" ;;
  29.         no-fb)            QUIRK_NOFB="true" ;;
  30.         pci-save)        QUIRK_PCI_SAVE="true" ;;
  31.         none)            QUIRK_NONE="true" ;;
  32.         *) continue ;;
  33.     esac
  34. done
  35.  
  36. reset_brightness()
  37. {
  38.     for bl in /sys/class/backlight/* ; do
  39.         [ -f "$bl/brightness" ] || continue
  40.         BR="$(cat $bl/brightness)"
  41.         echo 0 > "$bl/brightness"
  42.         echo "$BR" > "$bl/brightness"
  43.     done
  44. }
  45.  
  46. if command_exists vbetool; then
  47.     vbe() { vbetool "$@"; }
  48. else 
  49.     vbe() { echo "vbetool not installed!" 1>&2; return 1; }
  50. fi
  51.  
  52. if command_exists radeontool; then
  53.     radeon() { radeontool "$@"; }
  54. else 
  55.     radeon() { echo "radeontool not found" 1>&2; return 1; }
  56. fi
  57.  
  58. die_if_framebuffer() 
  59.     [ -d "/sys/class/graphics/fb0" ] || return
  60.     echo "--quirk-no-fb passed, but system is using a framebuffer."
  61.     echo "Aborting."
  62.     exit 1
  63. }
  64.  
  65.  
  66. save_fbcon()
  67. {
  68.     local con
  69.     for con in /sys/class/graphics/*/state; do
  70.         [ -f $con ] || continue
  71.         echo 1 >"${con}"
  72.     done
  73. }
  74.  
  75. resume_fbcon()
  76. {
  77.     local con
  78.     for con in /sys/class/graphics/*/state; do
  79.         [ -f $con ] || continue
  80.         echo 0 >"${con}"
  81.     done
  82. }
  83.  
  84. # Some tiny helper functions for quirk handling
  85. quirk() { [ "$1" = "true" ] && [ -z $QUIRK_NONE ]; }
  86.  
  87. # save/restore vbe state
  88. vbe_savestate() { vbe vbestate save |savestate vbestate; }
  89. vbe_restorestate() { restorestate vbestate |vbe vbestate restore; }
  90.  
  91. # save/restore the vbe mode
  92. vbe_savemode() { vbe vbemode get |savestate vbemode; }
  93. vbe_restoremode() 
  94. {
  95.     # this is a little mode complicated to handle special-casing mode 3.
  96.     local vbemode=$(restorestate vbemode)
  97.     if [ "$vbemode" = "3" ]; then
  98.         vbe vgamode set $vbemode
  99.     else 
  100.         vbe vbemode set $vbemode
  101.     fi
  102. }
  103.  
  104. # post the video card
  105. vbe_post() 
  106. {
  107.     local rom="/var/run/video.rom"
  108.     # if we do not have a romfile, do not post with it.
  109.     [ -f "$rom" ] || unset rom
  110.     vbe post $rom
  111.     sleep 0.1 
  112. }
  113.  
  114. # turn critical bits of radeon cards off/on
  115. radeon_off() { radeon dac off; radeon light off; }
  116. radeon_on() { radeon dac on; radeon light on; }
  117.  
  118. # save and restore video card PCI config state
  119. save_pci() 
  120. {
  121.     local pci="/sys/bus/pci/devices"
  122.     for dev in "${pci}"/*; do
  123.         [ -f "${dev}/class" ] || continue
  124.         [ $(cat "${dev}/class") = "0x030000" ] || continue
  125.         [ -f "${dev}/config" ] || continue
  126.         # it is a video card, it has a configuration.  Save it.
  127.         savestate "pci_video_${dev##*/}" <${dev}/config
  128.     done
  129. }
  130.  
  131. restore_pci() 
  132. {
  133.     local pci="/sys/bus/pci/devices"
  134.     for dev in "${pci}"/*; do
  135.         state_exists "pci_video_${dev##*/}" || continue
  136.         restorestate "pci_video_${dev##*/}" > "${dev}/config"
  137.     done
  138. }
  139.  
  140. suspend_video()
  141. {
  142.     # 0=nothing, 1=s3_bios, 2=s3_mode, 3=both
  143.     local acpi_flag=0
  144.     quirk "${QUIRK_S3_BIOS}" &&         acpi_flag=$(($acpi_flag + 1))
  145.     quirk "${QUIRK_S3_MODE}" &&         acpi_flag=$(($acpi_flag + 2))
  146.     sysctl -w kernel.acpi_video_flags=$acpi_flag
  147.     
  148.     quirk "${QUIRK_NOFB}" &&         die_if_framebuffer
  149.     quirk "${QUIRK_VBESTATE_RESTORE}" &&     vbe_savestate
  150.     quirk "${QUIRK_VBEMODE_RESTORE}" &&     vbe_savemode
  151.     quirk "${QUIRK_RADEON_OFF}" &&         radeon_off
  152.     quirk "${QUIRK_PCI_SAVE}" &&         pci_save
  153.     quirk "${QUIRK_VGA_MODE_3}" &&         vbe vbemode set 3
  154.     quirk "${QUIRK_DPMS_SUSPEND}" &&     vbe dpms suspend
  155.     save_fbcon
  156. }
  157. resume_video()
  158. {
  159.     # We might need to do one or many of these quirks
  160.     quirk "${QUIRK_PCI_SAVE}" &&         pci_restore
  161.     quirk "${QUIRK_VBE_POST}" &&         vbe_post
  162.     quirk "${QUIRK_VBESTATE_RESTORE}" &&     vbe_restorestate
  163.     quirk "${QUIRK_VBEMODE_RESTORE}" &&     vbe_restoremode
  164.     resume_fbcon     # also should be handled by a quirk.
  165.     quirk "${QUIRK_RADEON_OFF}" &&         radeon_on
  166.     quirk "${QUIRK_DPMS_ON}" &&         vbe dpms on
  167.     quirk "${QUIRK_RESET_BRIGHTNESS}" &&     reset_brightness
  168.     return 0  # avoid spurious hook exit failure message.
  169. }
  170.  
  171. help() {
  172.     echo  # first echo makes it look nicer.
  173.     echo "Video quirk handler options:"
  174.     echo
  175.     echo "  --quirk-dpms-on"
  176.     echo "  --quirk-dpms-suspend"
  177.     echo "  --quirk-radeon-off"
  178.     echo "  --quirk-reset-brightness"
  179.     echo "  --quirk-s3-bios"
  180.     echo "  --quirk-s3-mode"
  181.     echo "  --quirk-vbe-post"
  182.     echo "  --quirk-vbemode-restore"
  183.     echo "  --quirk-vbestate-restore"
  184.     echo "  --quirk-vga-mode3"
  185.     echo "  --quirk-none"
  186. }
  187.  
  188. case "$1" in
  189.     suspend) suspend_video ;;
  190.     hibernate)
  191.         if [ "$HIBERNATE_RESUME_POST_VIDEO" = "yes" ]; then
  192.             suspend_video
  193.         fi
  194.         ;;
  195.     resume) resume_video ;;
  196.     thaw)
  197.         if [ "${HIBERNATE_RESUME_POST_VIDEO}" = "yes" ]; then
  198.             resume_video
  199.         fi
  200.         ;;
  201.     help) help ;;
  202. esac
  203.